<?php

namespace app\common\library;

use AdaPaySdk\AdapayTools;
use AdaPaySdk\Payment as AdaPayment;
use AdaPaySdk\Refund;
use AdaPaySdk\PaymentConfirm;
use app\common\model\Order;
use think\Exception;

include_once  APP_PATH."../vendor/AdapaySdk/init.php";
// 在线支付模块
class Payment
{

    /**
     * 预下单
     */
    public static function create_order($params){
        \AdaPay\AdaPay::init(dirname(__FILE__). "/payment/config/merchantConfig.json", "live");
        $obj = new AdaPayment();
        $obj::$gateWayType = 'api';

        $obj_params = array(
            'app_id'=> $params['app_id'], // 应用ID
            'order_no'=> $params['order_sn'],
            'pay_channel'=> $params['pay_channel'],
            'time_expire'=> date("YmdHis", Order::getExpirationTime()),
            'pay_amt'=> $params['money'],
            'goods_title'=> $params['title'],
            'goods_desc'=> $params['goods_desc'],
//            'description'=> 'description',
            'device_info'=> ['device_ip'=> $params['ip']],
            'notify_url' => $params['notify_url'], // 回调地址
            'expend'    => $params['expend'],
        );
        $obj->create($obj_params);
        if ($obj->isError()){
            //失败处理
            if($obj->result){
                throw new Exception($obj->result['error_msg']);
            }else{
                throw new Exception("请求地址错误");
            }
        }
        // 成功处理
        $payment_order = $obj->result;
        return $payment_order;

    }

	// 退款
    public static function refund_order($params){
         \AdaPay\AdaPay::init(dirname(__FILE__). "/payment/config/merchantConfig.json", "live");
        $obj = new Refund();
        $obj::$gateWayType = 'api';
        $obj_params = array(
            # 原交易支付对象ID
            'payment_id'=> $params['payment_id'],
            # 退款订单号
            'refund_order_no'=> $params['refund_order_no'],
            # 退款金额
            'refund_amt'=> $params['refund_amt'],
            # 退款描述
            'reason'=> $params['reason'],
            'notify_url' => $params['notify_url'], // 回调地址
            # 扩展域
            'expend'=> '',
            # 设备静态信息
            'device_info'=> ''
        );
        $obj->create($obj_params);
        if ($obj->isError()){
            //失败处理
            if($obj->result){
                throw new Exception($obj->result['error_msg']);
            }else{
                throw new Exception("请求地址错误");
            }
        }
        $refund_order = $obj->result;
        return $refund_order;
//        print("创建退款对象".$obj->isError().'=>'.json_encode($obj->result)."\n");
    }
    /**
     * 验证签名
     * $data   验签请参data
     * $sign   验签请参sign
     */
    public static function verifySign($data, $sign){
//        \AdaPay\AdaPay::init(dirname(__FILE__). "/payment/config/merchantConfig.json", "live");
        //验签
        $tools = new AdapayTools();
        $checkSign = $tools->verifySign($data, $sign);
        if(!$checkSign){
            //验签失败逻辑
            throw new Exception("签名错误");
        }
        //验签成功逻辑

        return ;
    }


}